package web.Listener;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import settings.DAO.DicTypeDao;
import settings.DAO.DicValueDao;
import settings.domain.DicValue;
import settings.service.DicService;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.*;

public class SysInitListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext application=servletContextEvent.getServletContext();
        String config = "ApplicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        DicTypeDao dicTypeDao = (DicTypeDao) ac.getBean("dicTypeDao");
        DicValueDao dicValueDao = (DicValueDao) ac.getBean("dicValueDao");
        List<String> typeList=dicTypeDao.getDicType();
        for(String type:typeList){
            application.setAttribute(type,dicValueDao.getDicValueByType(type));
        }
        Map<String,String> map=new HashMap<>();
        ResourceBundle rb=ResourceBundle.getBundle("Stage2Possibility");
        Enumeration<String> stage= rb.getKeys();
        while (stage.hasMoreElements()){
            String s=stage.nextElement();
            map.put(s,rb.getString(s));
        }
        application.setAttribute("pMap",map);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
